﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HZ.Supervise.Model;

namespace HZ.Supervise.Web.PageControl
{
	public partial class StatisticsLeft : BasePage
	{
		HZ.Supervise.IBLL.IBLL_User ibllEntity = BLLFactory.BLL_User();

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				//add by 曹鹏 2011年5月7日15:42:29 取得当前用户
				lblCurrentUser.Text = HZ.Supervise.Web.Function.GetNowTime() + "&nbsp;&nbsp;<img alt='' src='../Images/ModalImageAh_New/smile.gif'/>&nbsp;&nbsp;" + CurrentAccount.GetUser().UserName;
				//取得当前用户角色
				this.lblCurrentRole.Text = CurrentAccount.GetUser().Org.OrgName;

				ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);

				AspNetPager1.Visible = true;
				AspNetPager2.Visible = true;
				if (!string.IsNullOrEmpty(this.link.Value))
				{
					//bangdingCompany(int.Parse(this.link.Value));
					bangdingCompany();
					bindHospital();
				}
				else
				{
					bangdingCompany();
					bindHospital();
				}
			}

		}

		private void bindHospital()
		{
			DataTable dt = new DataTable();
			HZ.Supervise.IBLL.IBLL_PreWarning pre = HZ.Supervise.BLLFactory.IBLL_PreWarning();
			string aredID = "";
			int i = 0;

			if (!CurrentAccount.GetUser().Org.Area.AreaID.Equals(Constants.CURRENT_DEPLOY_AREAID))
			{
				aredID = CurrentAccount.GetUser().Org.Area.AreaID;
			}
			if (string.IsNullOrEmpty(txtHosName.Text.Trim()))
			{
				dt = pre.GetHospital("", "X" + aredID, AspNetPager1.CurrentPageIndex, 8, out i);
			}
			else
			{
				dt = pre.GetHospital(txtHosName.Text.Trim(), aredID, 1, 8, out i);
			}
			AspNetPager1.RecordCount = i;

			gvwHospitalName.DataSource = dt;
			gvwHospitalName.DataBind();

			string sessionid = Guid.NewGuid().ToString();
			SuperviseQuery query = new SuperviseQuery();
			if (rblcompanytype.SelectedValue == "0")
			{
				query.CompanyType = 0;
			}
			else
			{
				query.CompanyType = 2;
			}

			Session[sessionid] = query;
		}

		/// <summary>
		/// 绑定企业
		/// </summary>
		private void bangdingCompany()
		{
			HZ.Supervise.IBLL.IBLL_PreWarning pre = HZ.Supervise.BLLFactory.IBLL_PreWarning();
			DataTable dt = new DataTable();

			string _CompanyID = DAL.DAL_Helper.Replace(txtname.Text.Trim());

			if (_CompanyID.Equals(""))
			{
				_CompanyID = "所有企业";
			}

			int totalrecord = 0;
			dt = pre.GetCompanyName(_CompanyID, rblcompanytype.SelectedValue, AspNetPager2.CurrentPageIndex, 8, out totalrecord);
			AspNetPager2.RecordCount = totalrecord;
			gvwCompany.DataSource = dt;
			gvwCompany.DataBind();
			//if (dt.Rows.Count == 0)
			//{
			//    Label1.Text = "暂无符合条件的数据";
			//}
			string sessionid = Guid.NewGuid().ToString();
			SuperviseQuery query = new SuperviseQuery();
			if (rblcompanytype.SelectedValue == "0")
			{
				query.CompanyType = 0;
			}
			else
			{
				query.CompanyType = 2;
			}

			Session[sessionid] = query;

			//this.Page.RegisterStartupScript("", "<script>alert"+dt+"</script>");
			//this.Page.RegisterStartupScript("", "<script> CallFunc('../Secondphase/ReportCompany/ReportOfCompanyFirstLevel.aspx?sessionid=' +" + sessionid + ", 'FORM', null, 'Main')</script>");
			//ClientScript.RegisterStartupScript(this.GetType(), "CallFunc", "<script> seturl('" + sessionid + "')</script>");


		}


		protected void AspNetPager1_PageChanged(object sender, EventArgs e)
		{
			bindHospital();
		}
		protected void AspNetPager2_PageChanged(object sender, EventArgs e)
		{
			ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "<script language='javascript'>show('Com');</script>");

			bangdingCompany();         //by张雄    2011.5.8

		}

		protected void benQuaryHos_Click(object sender, EventArgs e)
		{
			this.AspNetPager1.CurrentPageIndex = 1;
		}

		public string CheckMenuByUserID(string moduleName)
		{
			if (CurrentAccount.GetUser().UserID == "root")//如果是超级管理员，权限全开
			{

				return "";

			}
			else
			{

				string strCheck = "Check,No";

				IList<HZ.Supervise.Model.OperatingRule> list = ibllEntity.GetUserOperatingRule(CurrentAccount.GetUser().UserID);//得到该用户拥有的所有权限

				if (moduleName == "医院")
				{

					foreach (HZ.Supervise.Model.OperatingRule itemMenu in list)//遍历
					{

						if (itemMenu.OperatingRuleName.ToString() == "医院月报" || itemMenu.OperatingRuleName.ToString() == "医院季报")//匹配
						{

							strCheck = "HospitalCheckPass," + itemMenu.OperatingRuleName;

							break;

						}

					}
					return strCheck;

				}
				else if (moduleName == "地区")
				{
					foreach (HZ.Supervise.Model.OperatingRule itemMenu in list)//遍历
					{

						if (itemMenu.OperatingRuleName.ToString() == "地区月报" || itemMenu.OperatingRuleName.ToString() == "地区季报")//匹配
						{

							strCheck = "AreaCheckPass," + itemMenu.OperatingRuleName;

							break;

						}

					}
					return strCheck;
				}
				else if (moduleName == "企业")
				{
					foreach (HZ.Supervise.Model.OperatingRule itemMenu in list)//遍历
					{

						if (itemMenu.OperatingRuleName.ToString() == "企业月报" || itemMenu.OperatingRuleName.ToString() == "企业季报")//匹配
						{

							strCheck = "CompanyCheckPass," + itemMenu.OperatingRuleName;

							break;

						}

					}
					return strCheck;
				}


				return strCheck;

			}

		}

		protected void rblcompanytype_SelectedIndexChanged(object sender, EventArgs e)
		{

			ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "<script language='javascript'>show('Com');</script>");

			bangdingCompany();

		}

		protected void btnQueryCom_Click(object sender, EventArgs e)
		{
			this.AspNetPager2.CurrentPageIndex = 1;
		}

		protected void gvwCompany_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				string sessionid = Guid.NewGuid().ToString();
				SuperviseQuery query = new SuperviseQuery();
				query.CompanyName_PS = e.Row.Cells[0].Text.Trim();
				if (rblcompanytype.SelectedValue == "0")
				{
					query.CompanyType = 0;
				}
				else
				{
					query.CompanyType = 2;
				}
				string str = "CompanyName=" + HttpUtility.UrlEncode(query.CompanyName_PS) + "&CompanyType=" + query.CompanyType;

                e.Row.Cells[0].Text = "<a  style=\"cursor:hand\" onclick=\"a('" + this.gvwCompany.ClientID + "',this,'" + str + "');\">" + e.Row.Cells[0].Text + "</a>";

			}
		}

		protected void gvwHospitalName_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			//if (e.Row.RowType == DataControlRowType.DataRow)
			//{
			//    string sessionid = Guid.NewGuid().ToString();
			//    SuperviseQuery query = new SuperviseQuery();
			//    query.HospitalName = e.Row.Cells[0].Text.Trim();

			//    if (rblcompanytype.SelectedValue == "0")
			//    {
			//        query.CompanyType = 0;
			//    }
			//    else
			//    {
			//        query.CompanyType = 2;
			//    }
			//    Session[sessionid] = query;

			//    e.Row.Cells[0].Text = "<a  style=\"cursor:hand\" onclick=\"seturlHos('" + sessionid + "')\">" + e.Row.Cells[0].Text + "</a>";
			//}
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //string sessionid = Guid.NewGuid().ToString();
                //SuperviseQuery query = new SuperviseQuery();
                //query.HospitalID = e.Row.Cells[0].Text.Trim();
                //Session[sessionid] = query;
                string strHosID = HttpUtility.UrlEncode(e.Row.Cells[0].Text.Trim());
                e.Row.Cells[0].Text = "<a  style=\"cursor:hand\" onclick=\"a('" + this.gvwHospitalName.ClientID + "',this,'" + strHosID + "');\">" + e.Row.Cells[0].Text + "</a>";

            }
        }

        //protected void gvwHospitalName_RowCreated(object sender, GridViewRowEventArgs e)
        //{
        //    if (e.Row.RowType == DataControlRowType.DataRow)
        //    {

        //        e.Row.Attributes.Add("onclick", "a('"+this.gvwHospitalName.ClientID+"',this,'');");

        //    }
        //}

        //protected void gvwCompany_RowCreated(object sender, GridViewRowEventArgs e)
        //{
        //    if (e.Row.RowType == DataControlRowType.DataRow)
        //    {

        //        e.Row.Attributes.Add("onclick", "a('" + this.gvwCompany.ClientID + "',this,'');");
             
        //    }
        //}
	}
}
